package org.apache.ocean.main.search;


import org.apache.lucene.util.PriorityQueue;
import org.apache.ocean.main.search.Search.FieldDoc;
import org.apache.ocean.main.search.Search.Sort;

/**
 *
 * @author jasonr 
 */
public final class FieldSortedQueue extends PriorityQueue {
	private Sort sort;
  
  public FieldSortedQueue(Sort sort, int size) {
    this.sort = sort;
    initialize(size);
  }
  
  protected final boolean lessThan(final Object a, final Object b) {
  	FieldDoc docA = (FieldDoc)a;
  	FieldDoc docB = (FieldDoc)b;
    final int n = sort.size();
		int c = 0;
		for (int i=0; i < n && c==0; ++i) {
      c = docA.comparables[i].compareTo(docB.comparables[i]);
      if (sort.get(i).isReverse()) {
				c = -c;
			}
    }
    if (c == 0) return docA.doc > docB.doc;
    return c > 0;
  }
}
